package com.bsdk.install.device.lockLora.v1.cmd;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: com.hlslock.utils.lock
 * @Package: com.hlslock.utils.lock
 * @ClassName: Cmd
 * @Author: HansYoung
 * @Description: cmd
 * @Createtime: 2023/12/11 08:44
 * @Modifytime: 2023/12/14 20:50
 * @Version: 1.0
 * @Copyright: ©2006-2023 版权所有. 深圳豪力士
 */
@Slf4j
public class Cmd implements ICmd {
    /**
     * 构建密码
     *
     * @param pw       密码
     * @param pwMaxLen 密码最大长度
     * @return
     */
    @Override
    public String buildPassword(String pw, int pwMaxLen) {
        return new StringBuilder(pw).append("FFFFFFFFFFFFFF").substring(0, pwMaxLen);
    }


    //密码
    public int buildPassword(String pw, int pwMaxLen, byte[] bytes, int bytesStart) {

        String true_pw = new StringBuilder(pw).append("FFFFFFFFFFFFFF").substring(0, pwMaxLen);

        for (int i = 0; i < pwMaxLen / 2; i++) {
            bytes[bytesStart++] = (byte) Integer.parseInt(true_pw.substring(i * 2, (i * 2 + 2)), 16);
        }
        return bytesStart;
    }

    //卡
    public int buildCard(String pw, int pwMaxLen, byte[] bytes, int bytesStart) {
        //pw 最后一位转二进制 并把 2位和4位掉换
//        String endString = pw.substring(pw.length() - 1, pw.length());
//        String binary = Integer.toBinaryString(Integer.parseInt(endString));
//        String[] str1 = binary.split("");
//
//        String[] str2 = new String[]{str1[0], str1[3], str1[1], str1[2]};
//
//        String end =StringUtils.join(str2,"");
//        String newpw =pw.substring(0,pw.length() - 1) + Integer.toString(Integer.parseInt(end,2),16).toUpperCase();
        String true_pw = new StringBuilder(pw).append("00000000000000").substring(0, pwMaxLen);

        for (int i = 0; i < pwMaxLen / 2; i++) {
            bytes[bytesStart++] = (byte) Integer.parseInt(true_pw.substring(i * 2, (i * 2 + 2)), 16);
        }
        return bytesStart;
    }

    //身份证
    public int buildIdCard(String pw, int pwMaxLen, byte[] bytes, int bytesStart) {
        String true_pw = new StringBuilder(pw).append("FFFFFFFFFFFFFF").substring(0, pwMaxLen);

        for (int i = 0; i < pwMaxLen / 2; i++) {
            bytes[bytesStart++] = (byte) Integer.parseInt(true_pw.substring(i * 2, (i * 2 + 2)), 16);
        }
        return bytesStart;
    }
    @Override
    public int buildUserId(int userId, byte[] data, int dataStart) {
        data[dataStart++] = (byte) (userId / 0x100);
        data[dataStart++] = (byte) (userId % 0x100);
        return dataStart;
    }

    @Override
    public int buildTime(String time, byte[] data, int dataStart) {
        //时间
        for (int i = 0; i < 6; i++) {

            data[dataStart++] = (byte) Integer.parseInt(time.substring(i * 2, (i * 2 + 2)), 10);

        }
        return dataStart;
    }
    @Override
    public String buildTimeString(byte[] buf,int dataStart){
        int int_tmp,j=dataStart;
        StringBuilder tmp = new StringBuilder();

        int_tmp = 2000 + buf[j++];
        tmp.append(String.format("%04d", int_tmp));//年
        tmp.append("-");
        int_tmp = buf[j++];
        tmp.append(String.format("%02d", int_tmp));//月
        tmp.append("-");
        int_tmp = buf[j++];
        tmp.append(String.format("%02d", int_tmp));//日
        tmp.append(" ");
        int_tmp = buf[j++];
        tmp.append(String.format("%02d", int_tmp));//时
        tmp.append(":");
        int_tmp = buf[j++];
        tmp.append(String.format("%02d", int_tmp));//分
        tmp.append(":");
        int_tmp = buf[j++];
        tmp.append(String.format("%02d", int_tmp));//秒
        return tmp.toString();
    }
    /**
     * 字符串转字节
     *
     * @param s
     * @return
     */
    @Override
    public byte[] toBytes(String s) {
        byte[] bytes = new byte[s.length() / 2];
        for (int i = 0; i < s.length() / 2; i++) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
        }
        return bytes;
    }

    @Override
    public byte[] Valid_toBytes(String s,byte type)
    {
        int i,len;
        /*
  循环标志	4 bytes		天循环:FF FF FF FF 08 1E 14 1E //8:30-20:30
周循环:FF FF FF xx 08 1E 14 1E //xx由位定义bit0:周日 bit1:周一... bit6:周六 bit7:0 如不考虑时段请填充FF
03月循环:xx xx xx xx 08 1E 14 1E //xx由位定义bit0:1号 bit1:2号... bit0:1号

         */
        int mask,tmp;
        String str_tmp = "";
        byte[] bytes = new byte[4];
        for(i = 0;i<4;i++) bytes[i] = (byte)0xFF;
        len = s.length();
        if(len>32)
        {
            return bytes;
        }
        for(i = 0;i<4;i++) bytes[i] = (byte)0x00;
        mask = 0x00;
        for (i = 0; i < len; i++) {
            str_tmp = s.substring(i,i+1);
            log.info(str_tmp);
            if (str_tmp.equals("1")) mask = mask|(1<<i);
            log.info("AAAAAA"+String.valueOf(mask));
        }

        if(type == 0x02)//week
        {
            if(len == 7)
            {
                bytes[3] = (byte)(mask&0xFF);
            }
        }
        else if(type == 0x03) //month
        {
            if(s.length()>26) {
                bytes[3] = (byte)(mask/0x100);
                bytes[2] = (byte)(mask/0x10000);
                bytes[1] = (byte)(mask/0x1000000);
                tmp = mask/0x1000000;
                bytes[0] = (byte)(tmp/0x100);
            }
        }
        else //day an FF无需处理
        {
            for(i = 0;i<4;i++) bytes[i] = (byte)0xFF;
        }
        return bytes;

    }

    @Override
    public byte check(byte[] byteData, int len) {
        byte xor = 0;
        for (byte b : byteData) {
            xor ^= b;
        }
        return xor;
    }
    @Override

    public byte check_crc(byte[] ptr, int n)
    {
        int j = 8, i = 0;
        int cbit, out1;
        int crc = 0x79, crc_a = 0, crc_b = 0;
        do
        {
            crc_a = ptr[i++];

            j = 8;
            cbit = 1;
            do
            {
                crc_b = crc_a;
                crc_b = crc_b ^ crc;
                out1 = crc_b & cbit;
                //	crc_b=crc_b>>1;
                if (out1 != 0)
                {
                    crc = crc ^ 0X18;
                    crc = crc >> 1;
                    crc |= 0x80;
                }
                else
                    crc = crc >> 1;
                crc_a = crc_a >> 1;
                //	cbit=cbit<<1;
            } while (--j > 0);
        } while (--n > 0);
        return (byte)crc;
    }





}